package leetcode.editor.offer;

// 剑指 Offer 49. 丑数
// https://leetcode.cn/problems/chou-shu-lcof/description/
class ChouShuLcof {
    public static void main(String[] args) {
        Solution solution = new ChouShuLcof().new Solution();
        solution.nthUglyNumber(10);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int nthUglyNumber(int n) {
            int a = 0, b = 0, c = 0;
            // dp[i] 代表第 i + 1 个丑数
            int[] dp = new int[n];
            // 初始化，第一个丑数为1
            dp[0] = 1;
            for (int i = 1; i < n; i++) {
                int n1 = dp[a] * 2, n2 = dp[b] * 3, n3 = dp[c] * 5;
                dp[i] = Math.min(Math.min(n1, n2), n3);
                if (dp[i] == n1) a++;
                if (dp[i] == n2) b++;
                if (dp[i] == n3) c++;
            }

            return dp[n - 1];
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
